package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.*;
import com.ylyq.ylxx.model.request.*;
import com.ylyq.ylxx.service.IOceanegineCreativeService;
import com.ylyq.ylxx.util.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName OceanegineCreativeServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/1 14:11
 * @Version 1.0
 */
@Service
public class OceanegineCreativeServiceImpl implements IOceanegineCreativeService {
    @Override
    public ResponseData getCreative(GetCreativeRequest getCreativeRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "creative/get/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getCreativeRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreativeInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreativeInfo.class);
        return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
    }

    @Override
    public OceanengineCreateCreativeResult createCreative(CreateCreativeRequest createCreativeRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "creative/create_v2/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSON(createCreativeRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreateCreativeResult oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreateCreativeResult.class);
        return oceanengineAccount;
    }

    @Override
    public OceanengineUpdateCreativeResult updateCreative(UpdateCreativeRequest createCreativeRequest) {

        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "creative/update_v2/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSON(createCreativeRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineUpdateCreativeResult oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineUpdateCreativeResult.class);
        return oceanengineAccount;
    }

    @Override
    public OceanengineCreativeDetail creativeDetail(CreativeDetailRequest creativeDetailRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "creative/read_v2/";
        Map<String, Object> data = new HashMap();
        if (null != creativeDetailRequest.getAdvertiser_id()) {
            data.put("advertiser_id", creativeDetailRequest.getAdvertiser_id());
        }
        if (null != creativeDetailRequest.getAd_id()) {
            data.put("ad_id", creativeDetailRequest.getAd_id());
        }
        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSONString(data), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreativeDetail oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreativeDetail.class);
        return oceanengineAccount;
    }

    @Override
    public ResponseData updateCreativeState(UpdateCreativeState updateCreativeState) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "creative/update/status/";
        Map<String, Object> data = new HashMap();
        if (null != updateCreativeState.getAdvertiser_id()) {
            data.put("advertiser_id", updateCreativeState.getAdvertiser_id());
        }
        if (null != updateCreativeState.getCreative_ids() && updateCreativeState.getCreative_ids().length > 0) {
            data.put("creative_ids", updateCreativeState.getCreative_ids());
        }
        if (StringUtils.isNotBlank(updateCreativeState.getOpt_status())) {
            data.put("opt_status", updateCreativeState.getOpt_status());
        }
        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSONString(data), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineUpdateCreativeState oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineUpdateCreativeState.class);
        return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
    }

    @Override
    public ResponseData creativematerialInfo(CreativematerialInfo creativematerialInfo) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "creative/material/read/";
        Map<String, Object> data = new HashMap();
        if (null != creativematerialInfo.getAdvertiser_id()) {
            data.put("advertiser_id", creativematerialInfo.getAdvertiser_id());
        }
        if (null != creativematerialInfo.getCreative_ids() && creativematerialInfo.getCreative_ids().length > 0) {
            data.put("creative_ids", creativematerialInfo.getCreative_ids());
        }
        if (null != creativematerialInfo.getFields() && creativematerialInfo.getFields().length > 0) {
            data.put("fields", creativematerialInfo.getFields());
        }

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSONString(data), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreativematerialInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreativematerialInfo.class);
        return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
    }

    @Override
    public ResponseData creativeRejectreason(CreativeRejectreason creativematerialInfo) {
        // 请求地址
        String open_api_url_prefix = "https://ad.toutiao.com/open_api/2/";
        String uri = "ad/reject_reason/";
        Map<String, Object> data = new HashMap();
        if (null != creativematerialInfo.getAdvertiser_id()) {
            data.put("advertiser_id", creativematerialInfo.getAdvertiser_id());
        }
        if (null != creativematerialInfo.getCreative_ids() && creativematerialInfo.getCreative_ids().length > 0) {
            data.put("creative_ids", creativematerialInfo.getCreative_ids());
        }

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSONString(data), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineCreativeRejectreason oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineCreativeRejectreason.class);
        return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
    }
}
